package com.lyn.module_custom_dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import androidx.annotation.NonNull;

import com.lyn.module_custom_base.dialog.BaseDialog;
import com.lyn.module_custom_dialog.databinding.DialogPassKeyboardBinding;


public class PasswordDialogDiy extends BaseDialog<DialogPassKeyboardBinding> implements View.OnClickListener, TextWatcher {
    DialogPassKeyboardBinding binding;
    private OnClickItemListener listener;

    private String password="";

    public PasswordDialogDiy(@NonNull Context context, OnClickItemListener listener) {
        super(context, R.style.common_dialog);
        this.listener = listener;
    }

    private String hint;

    public void setHint(String hint) {
        this.hint = hint;
    }

    @Override
    protected DialogPassKeyboardBinding getViewBinding() {
        return DialogPassKeyboardBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void bindData(DialogPassKeyboardBinding binding, Dialog dialog) {
        this.binding = binding;
        if (!TextUtils.isEmpty(hint)){
            binding.Password.setHint(hint);
        }
        binding.Password.addTextChangedListener(this);
        binding.Password.setEnabled(false);

        binding.KeyBoard.Exit.setOnClickListener(this);
        binding.KeyBoard.Zero.setOnClickListener(this);
        binding.KeyBoard.Clear.setOnClickListener(this);

        binding.KeyBoard.One.setOnClickListener(this);
        binding.KeyBoard.Two.setOnClickListener(this);
        binding.KeyBoard.Three.setOnClickListener(this);

        binding.KeyBoard.Four.setOnClickListener(this);
        binding.KeyBoard.Fives.setOnClickListener(this);
        binding.KeyBoard.Six.setOnClickListener(this);

        binding.KeyBoard.Seven.setOnClickListener(this);
        binding.KeyBoard.Eight.setOnClickListener(this);
        binding.KeyBoard.Nine.setOnClickListener(this);


    }

    @Override
    protected void bindListener(DialogPassKeyboardBinding binding, Dialog dialog) {
    }

    @Override
    protected boolean getDispatchKeyEvent() {
        return true;
    }

    @Override
    protected boolean customLengthAndWidth() {
        return false;
    }

    @Override
    public void onClick(View v) {
        int number = -1;
        int id = v.getId();
        if (id == R.id.Exit) {
            dismiss();
        } else if (id == R.id.Clear) {
            if (!TextUtils.isEmpty(password)) {
                password = password.substring(0, password.length() - 1);
            }
        } else if (id == R.id.Zero) {
            number = 0;
        } else if (id == R.id.One) {
            number = 1;
        } else if (id == R.id.Two) {
            number = 2;
        } else if (id == R.id.Three) {
            number = 3;
        } else if (id == R.id.Four) {
            number = 4;
        } else if (id == R.id.Fives) {
            number = 5;
        } else if (id == R.id.Six) {
            number = 6;
        } else if (id == R.id.Seven) {
            number = 7;
        } else if (id == R.id.Eight) {
            number = 8;
        } else if (id == R.id.Nine) {
            number = 9;
        }
        if (number != -1 && password.length() < 6) {
            password = password + number;
        }
        binding.Password.setText(password);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (listener != null) {
            String string = s.toString();
            boolean onItem = listener.onItem(string, binding.getRoot(),this);
            if (!onItem) {
                password = "";
                binding.Password.setText("");
            }
        }
    }

    public interface OnClickItemListener {
        boolean onItem(String password, final View view, Dialog dialog);
    }
}
